package com.hxy.health.platform.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.health.platform.common.Const;
import com.hxy.health.platform.pojo.Cart;
import com.hxy.health.platform.pojo.Product;
import com.hxy.health.platform.pojo.User;
import com.hxy.health.platform.service.CartService;
import com.hxy.health.platform.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired

    private ProductService productService;

    /**
     * 跳转到购物车界面
     */
    @RequestMapping("page/cart")
    public String cartPage(HttpServletRequest request,
                           @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "loginOrRegister";
        }
        PageInfo<Cart> cartPageInfo = cartService.getByEmail(pageNum, pageSize, user.getEmail());
        String totalPrice = TotalPriceUtil(cartPageInfo.getList());
        modelMap.addAttribute("totalPrice", totalPrice);
        modelMap.addAttribute("cartPageInfo", cartPageInfo);
        return "cart";
    }

    // 计算总和
    private String TotalPriceUtil(List<Cart> list) {
        double totalPrice = 0.00;
        for (Cart cart : list) {
            totalPrice = totalPrice + Double.parseDouble(cart.getProductTotalPrice());
        }
        return String.valueOf(totalPrice);
    }

    /**
     * 添加购物车
     *
     * @param request
     * @return
     */

    @RequestMapping("cart/addCart")
    public String addCart(HttpServletRequest request,
                          @RequestParam("productId") Integer productId,
                          @RequestParam(value = "quantity", defaultValue = "1") Integer quantity,
                          ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "loginOrRegister";
        }
        Product product = productService.getById(productId);
        int result = cartService.updateCart(product, quantity, user.getEmail());
        if (result > 0) {

            return "redirect:/page/cart";
        }
        return "";
    }

    /**
     * 手动添加或者删除
     */

    @RequestMapping("cart/addCartByQuantity")
    public String addCartByQuantity(@RequestParam("productId") Integer productId,
                                    @RequestParam("quantity") Integer quantity,
                                    @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam("productPrice") Double productPrice,
                                    ModelMap modelMap, HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "loginOrRegister";
        }
        cartService.updateTotalPrice(productId, quantity, productPrice);
        PageInfo<Cart> cartPageInfo = cartService.getByEmail(pageNum, pageSize, user.getEmail());
        modelMap.addAttribute("cartPageInfo", cartPageInfo);
        String totalPrice = TotalPriceUtil(cartPageInfo.getList());
        modelMap.addAttribute("totalPrice", totalPrice);
        return "cart";

    }

    /**
     * 删除购物车商品
     */
    @RequestMapping("cart/deleteCart")
    public String deleteCart(HttpServletRequest request, @RequestParam("cartId") Integer cartId,
                             @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             ModelMap modelMap) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        int result = cartService.deleteCartById(cartId);
        if (result > 0) {
            PageInfo<Cart> cartPageInfo = cartService.getByEmail(pageNum, pageSize, user.getEmail());
            modelMap.addAttribute("cartPageInfo", cartPageInfo);
            String totalPrice = TotalPriceUtil(cartPageInfo.getList());
            modelMap.addAttribute("totalPrice", totalPrice);
            return "cart";
        }
        return "";
    }

    /**
     * 跳转到结算页面
     */
    @RequestMapping("page/checkout")
    public String checkout( @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            HttpServletRequest request, ModelMap modelMap) {
        User user= (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        PageInfo<Cart> cartPageInfo = cartService.getByEmail(pageNum, pageSize, user.getEmail());
        modelMap.addAttribute("cartPageInfo", cartPageInfo);
        String totalPrice = TotalPriceUtil(cartPageInfo.getList());
        modelMap.addAttribute("totalPrice", totalPrice);
        return "checkout";
    }

}
